package com.example.myspringpro.util;

import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Random;

/**
 * @Description
 * @Author allenhu <huxiangneng@163.com>
 * @Date 2019/05/09
 * @Version V1.0.0
 **/
public class ToolUtil {
    //密码加密
    public static String md5(String password) {
        String newPassword = password;
        return DigestUtils.md5DigestAsHex(newPassword.getBytes());
    }
    //salt 生成
    private static String random(int mu){
        Random random = new Random();
        String chars = "qazwsxedcrfvtgbyhnujmikolp";
        String returns = "";
        for(int i = 0 ; i < mu ;i ++ ){
            returns += chars.charAt(random.nextInt(chars.length()));
        }
        return returns;
    }

    public static boolean isAjax(HttpServletRequest request) {
        return (request.getHeader("X-Requested-With") != null &&
                "XMLHttpRequest".equals(request.getHeader("X-Requested-With").toString()));
    }

    /**
     * 生成唯一主键
     * 格式： 时间+随机数
     * @return
     */
    public static synchronized String genUniqueKey() {
        Random random = new Random();
        Integer a = random.nextInt(900000) + 100000;
        return System.currentTimeMillis() + String.valueOf(a);
    }
}
